<?php

class EmailGenerator_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}
	
	function get_user_with_newsletter() {
		$sql = "SELECT * FROM user WHERE is_deleted=0 AND newsletter_day > 0 AND CHAR_LENGTH(email) != 0";
		$query = $this->db->query($sql);
		$data = array();
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}
		}
		return $data;
	}
	
	function get_new_notification($fb_id, $last_newsletter) {
		$sql = "SELECT * FROM notification WHERE target_fb_id = ? AND is_new = 1";
		$query = $this->db->query($sql, array($fb_id));
		$data = array() ;
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}
		}
		return $data;
	}
	
	function get_new_request($fb_id, $last_newsletter) {
		$count_new = 0; // count total new requests
		$sql = "SELECT * FROM offer WHERE fb_id = ? ";
		$query = $this->db->query($sql, array($fb_id));
		$offer = array() ;
		if ($query->num_rows() > 0) {
			$i = -1;
			foreach ($query->result_array() as $row) {
				$offer[++$i] = $row;
				$sql2 = "SELECT fb_id FROM request WHERE offer_id = ? AND date_created > ?";
				$query2 = $this->db->query($sql2, array($row['offer_id'], $last_newsletter));
				$offer[$i]['requester'] = array();
				if ($query2->num_rows() > 0) {
					$count_new += $query2->num_rows();
					foreach ($query2->result_array() as $row2) {
						$offer[$i]['requester'][] = $row2;
					}
				}
			}
		}
		$data['total'] = $count_new;
		$data['offer'] = $offer;
		return $data;
	}
	
	function get_new_offer($last_newsletter) {
		$sql = "SELECT * FROM offer WHERE date_created > ? AND status='Available' ORDER BY item_price";
		$query = $this->db->query($sql, array($last_newsletter));
		$data = array();
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}
		}
		return $data;
	}
	
	function get_new_comment($fb_id, $last_newsletter) {
		$count_new = 0; // count total new requests
		$sql = "SELECT * FROM offer WHERE fb_id = ? ";
		$query = $this->db->query($sql, array($fb_id));
		$offer = array() ;
		if ($query->num_rows() > 0) {
			$i = -1;
			foreach ($query->result_array() as $row) {
				$offer[++$i] = $row;
				$sql2 = "SELECT poster_fb_id, message FROM offer_chat WHERE offer_id = ? AND poster_fb_id != ? AND date_created > ? ";
				$query2 = $this->db->query($sql2, array($row['offer_id'], $fb_id, $last_newsletter));
				$offer[$i]['comment'] = array();
				if ($query2->num_rows() > 0) {
					$count_new += $query2->num_rows();
					foreach ($query2->result_array() as $row2) {
						$offer[$i]['comment'][] = $row2;
					}
				}
			}
		}
		$data['total'] = $count_new;
		$data['offer'] = $offer;
		return $data;
	}
	
	
	function insert_email($fb_id, $message, $email){
		$sql = "INSERT INTO email (fb_id, message, email) VALUES (?, ?, ?)";
		$query = $this->db->query($sql, array($fb_id, $message, $email));
	}
	
	function delete_email($email_id){
		$sql = "DELETE FROM email WHERE email_id = ?";
		$query = $this->db->query($sql, $email_id);
	}
	
	function get_emails(){
		//LIMIT is 50
		$sql = "SELECT * FROM email WHERE 1 ORDER BY email_id LIMIT 0, 50";
		$query = $this->db->query($sql);
		
		$data = array();
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}
		}
		return $data;
	}
	

}
?>